package bb.lanxing.util;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import bb.lanxing.R;

public class CountdownAnimationUtil {
    // 默认计时
    private static final int DEFAULT_REPEAT_COUNT = 4;
    // 最后一秒显示的文本
    private static final String LAST_SECOND_TEXT = "Go";

    // 当前的计时
    private static int sCurCount = DEFAULT_REPEAT_COUNT;

    /**
     * @param countdown       要被倒计时的控件
     * @param countdownLayout layout
     */
    public static void start(LinearLayout countdownLayout, TextView countdown, Context context) {
        // 设置计时
        sCurCount = DEFAULT_REPEAT_COUNT - 1;
        MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.wav3);
        mediaPlayer.start();
        countdown.setText(String.valueOf(sCurCount));
        countdownLayout.setVisibility(View.VISIBLE);

        // 透明度渐变动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        // 缩放渐变动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0.1f, 1.3f, 0.1f, 1.3f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setRepeatCount(sCurCount);
        alphaAnimation.setRepeatCount(sCurCount);
        alphaAnimation.setDuration(1000);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // 动画结束时，隐藏
                countdownLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // 减秒
                --sCurCount;
                // 设置文本
                if (sCurCount == 0) {
                    countdown.setText(LAST_SECOND_TEXT);
                } else {
                    countdown.setText(String.valueOf(sCurCount));
                }
                MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.wav3);
                switch (sCurCount) {
                    case 2:
                        mediaPlayer = MediaPlayer.create(context, R.raw.wav2);
                        break;
                    case 1:
                        mediaPlayer = MediaPlayer.create(context, R.raw.wav1);
                        break;
                    case 0:
                        mediaPlayer = MediaPlayer.create(context, R.raw.go);
                        break;
                }
                mediaPlayer.start();
            }
        });
        // 两个动画同时播放
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(scaleAnimation);
        countdown.startAnimation(animationSet);
    }
}